package br.com.minhascontas.activity.relatorio;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import br.com.example.minhascontas.R;
import br.com.minhascontas.DAO.MinhasContasOpenHelper;
import br.com.minhascontas.model.Grupo;
import br.com.minhascontas.model.ItemGrupo;

public class RelatorioPorItem extends Activity{
	
	private MinhasContasOpenHelper dataSource = new MinhasContasOpenHelper(this);
	
	private Grupo clsGrupo = new Grupo();
	private ItemGrupo clsItemGrupo = new ItemGrupo();
	
	private Spinner spGrupo;
	private Spinner spItem;
	private Button btnRelatorioPorItem;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.relatorio_por_item);
		
		spGrupo = (Spinner) findViewById(R.id.spGrupo);
		spItem = (Spinner) findViewById(R.id.spItem);
		btnRelatorioPorItem = (Button) findViewById(R.id.btnRelatorioPorItem);
		
		populaSpinnerGrupo();
		
		
		spGrupo.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View view,
					int pos, long id) {
				// TODO Auto-generated method stub
				clsGrupo = (Grupo) adapter.getItemAtPosition(pos);

				// popula spItem somente com itens pertencentes ao grupo
				// escolhido
				int intIdGrupo = (int) (long) clsGrupo.getId();
				populaSpinnerItem(intIdGrupo);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		spItem.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View view,
					int pos, long id) {
				// TODO Auto-generated method stub
				clsItemGrupo = (ItemGrupo) adapter.getItemAtPosition(pos);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		
		btnRelatorioPorItem.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				String strIdItem = Long.toString(clsItemGrupo.getId());
				Intent intent = new Intent(RelatorioPorItem.this, ExibirLancamentos.class);
				intent.putExtra("item", strIdItem);
				startActivity(intent);
			}
		});
		
		
	}
	
	private void populaSpinnerGrupo() {
		List<Grupo> labels = dataSource.listarGrupo();

		ArrayAdapter<Grupo> dataAdapter = new ArrayAdapter<Grupo>(this,
				android.R.layout.simple_list_item_1, labels);

		dataAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spGrupo.setAdapter(dataAdapter);
		dataSource.close();
	}
	
	private void populaSpinnerItem(int intIdGrupo) {
		List<ItemGrupo> labels = dataSource
				.listarItemGrupoByGrupoId(intIdGrupo);

		ArrayAdapter<ItemGrupo> dataAdapter = new ArrayAdapter<ItemGrupo>(this,
				android.R.layout.simple_list_item_1, labels);

		dataAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spItem.setAdapter(dataAdapter);
		dataSource.close();
	}

}
